<?php
App::uses('AppController', 'Controller');
/**
 * Articles Controller
 *
 * @property Article $Article
 * @property PaginatorComponent $Paginator
 */
class ArticlesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('index','view');
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$SITEID = Configure::read('SITEID');
		
		$this->Article->recursive = 0;
		$conditions = $this->indexSearch();
		$this->Paginator->settings = array(
				'conditions'=>	$conditions['conditions'],
				'order'=>$conditions['order'],
				'limit'=>10
		);
		$this->Article->unbindModel(array(
				'belongsTo'=>array('SysWebsite','SysSeo','User')
		),true);
		$this->set('articles', $this->Paginator->paginate('Article'));
		
		// 分类-------------------------------------------------------------
		$this->loadModel('ArticleCate');
		$siteArticleCate = $this->ArticleCate->getSiteEnable($SITEID);
		$this->set('siteArticleCate',$siteArticleCate);
		
	}
	
	private function indexSearch(){
		$order = isset($this->request->named['sort'])?$this->request->named['sort']:false;
		$cate = isset($this->request->params['cate'])?$this->request->params['cate']:false;
		//$type = isset($this->request->named['type'])?$this->request->named['type']:false;
		
		$SITEID = Configure::read('SITEID');
		$conditions = array();
		$conditions['Article.sys_website_id'] = $SITEID;
		$conditions['Article.enable'] = 1;
	
		if(false){
			if('a' == 'hot'){
				$conditions['Article.recmd_hot'] = 1;
			}
		}
		
		if($cate){
			$conditions['Article.article_cate_id'] = $cate;
			
			$this->loadModel('ArticleCate');
			$this->ArticleCate->recursive = 1;
			$ac = $this->ArticleCate->findById($cate);
			$this->set('articleCate',$ac);
			$seo_title		 = $ac['SysSeo']['title'];
			$seo_keywords	 = $ac['SysSeo']['keywords'];
			$seo_description = $ac['SysSeo']['description'];
			
			$this->set('seo_title',$seo_title);
			$this->set('seo_keywords',$seo_keywords);
			$this->set('seo_description',$seo_description);
		}else{
		}
		
		if($order){
			if($order == 'hot'){
				$sort = 'Article.recmd_hot DESC,Article.publish DESC,Article.sort DESC';
			}else{
				
			}
		}else{
			$sort = 'Article.created DESC';
		}
		$sparams = array('cate'=>$cate,'sort'=>$order);
		$this->set('sparams',$sparams);
		
		$this->set('seo_title_ext',' - 演出百科【秦文票务】');
		return array('conditions'=>$conditions,'order'=>$sort);
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		
		if (!$this->Article->exists($id)) {
			throw new NotFoundException(__('Invalid article'));
		}
		// 分类-------------------------------------------------------------
		$SITEID = Configure::read('SITEID');
		$siteArticleCate = $this->Article->ArticleCate->getSiteEnable($SITEID);
		$this->set('siteArticleCate',$siteArticleCate);
		
		
		$options = array('conditions' => array('Article.' . $this->Article->primaryKey => $id));
		$article = $this->Article->find('first', $options);
		$count = $article['Article']['count_view']+1;
		$ca = array(
			'Article'=>array(
				'id'=>$id,
				'count_view'=>$count
			)	
		);
		$this->Article->save($ca);
		$this->set('article', $article);
		
		$neighbors = $this->Article->find(
				'neighbors',
				array(
						'field' => 'id',
						'value' => $article['Article']['id'],
						'conditions' => array(
								'Article.article_cate_id'=>$article['Article']['article_cate_id']
						)
				)
		);
		$this->set('neighbors',$neighbors);
		
		$seo_title		 = $article['SysSeo']['title'];
		$seo_keywords	 = $article['SysSeo']['keywords'];
		$seo_description = $article['SysSeo']['description'];
		$this->set('seo_title',$seo_title);
		$this->set('seo_keywords',$seo_keywords);
		$this->set('seo_description',$seo_description);
		$this->set('seo_title_ext',' - 演出订票【秦文票务】');
		
		$relateArticle = $this->Article->relateArticle($article['Article']['tags'],5,$article['Article']['id']);
		$this->set('relateArticle',$relateArticle);
		
		$newArticle = $this->Article->newArticle(6);
		$this->set('newArticle',$newArticle);
		
		$this->loadModel('Ticket');
		$relateTicket = $this->Ticket->relateTicket($article['Article']['tags'],5);
		$this->set('relateTicket',$relateTicket);
		
		$tuijian = $this->Ticket->recommendTicket(6);
		$this->set('tuijianT',$tuijian);
		
		$newTicket = $this->Ticket->newTicket(6);
		$this->set('newTicket',$newTicket);
		
		// 票品分类-------------------------------------------------------------
		$SITEID = 1;
		$this->loadModel('TicketCate');
		$siteTicketCate = $this->TicketCate->getSiteEnable($SITEID);
		$this->set('siteTicketCate',$siteTicketCate);
	}


/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Article->recursive = 0;
		$action = array('admin' => true,'controller' => 'Articles','action'=>'jsonPage');
		$param = $this->Article->pageParam($action);
		$this->set('paginationScript',$this->Article->showGridPagination($param));
	}
	
	/**
	 * function
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-21 下午5:05:53
	 * @param variable
	 * @return
	 */
	function admin_jsonPage($type = false){
		$this->Article->recursive = 0;
		$this->autoRender = false;
		$searchtype = $this->request->data('_search');
		$conditions = array();
		if($searchtype=='true'){
			$conditions = $this->search_condition($this->request->data);
		}
		
		$conditions['Article.enable']=1;
		$conditions['Article.sys_website_id']= $this->Session->read('SITEID');
	
		$config = array(
				'_search'=>$searchtype,
				'page'=>$this->request->data('page'),
				'rows'=>$this->request->data('rows'),
				'sidx'=>$this->request->data('sidx'),
				'sord'=>$this->request->data('sord')
		);
		$res = $this->Article->pagination($config,array(),$conditions);
		return $this->Article->jsonData($res);
	}
	/**
	 * 查询条件
	 * @author zmx@gootop.net
	 * @Date 2011-2-23
	 * @return
	 */
	private function search_condition($param){
		$search['Article'] = $param['Article'];
		$c = $this->postConditions($search);
		$return = array();
		if($c['Article.id']){
			$return['Article.id'] = $c['Article.id'];
		}else if($c['Article.title']){
			$return['Article.title like'] = '%'.$c['Article.title'].'%';
		}
		return $return;
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->Article->exists($id)) {
			throw new NotFoundException(__('Invalid article'));
		}
		$options = array('conditions' => array('Article.' . $this->Article->primaryKey => $id));
		$this->set('article', $this->Article->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Article->create();
			if ($this->Article->save($this->request->data)) {
				$this->Session->setFlash(__('The article has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
			}
		}
		$articleCates = $this->Article->ArticleCate->getSiteEnable($this->Session->read('SITEID'));
		$this->set(compact('articleCates'));
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Article->exists($id)) {
			throw new NotFoundException(__('Invalid article'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Article->save($this->request->data)) {
				$this->Session->setFlash(__('The article has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Article.' . $this->Article->primaryKey => $id));
			$this->request->data = $this->Article->find('first', $options);
		}
		$articleCates = $this->Article->ArticleCate->getSiteEnable($this->Session->read('SITEID'));
		$this->set(compact('articleCates'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Article->id = $id;
		if (!$this->Article->exists()) {
			throw new NotFoundException(__('Invalid article'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Article->delete()) {
			$this->Session->setFlash(__('The article has been deleted.'));
		} else {
			$this->Session->setFlash(__('The article could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
	
	public function admin_close($id = null) {
		$this->Article->id = $id;
		if (!$this->Article->exists()) {
			throw new NotFoundException(__('Invalid ticket'));
		}
		//$this->request->onlyAllow('post', 'delete');
		$this->Article->set('enable',2);
		if ($this->Article->save()) {
			$this->Session->setFlash(__('The article has been deleted.'),'tip/tip_float',array('t'=>'success'));
		} else {
			$this->Session->setFlash(__('The article could not be deleted. Please, try again.'),'tip/tip_float');
		}
		return $this->redirect(array('action' => 'index'));
	}
}
